<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>08_子传父$emit的使用</title>
</head>
<!-- 
    本章主要讲解: $emit的使用
    有的时候我们需要收集子组件的数据或者监听一些它的状态 【然后传递给父组件】,于是就有了 $emit
-->

<body>
    <div id="app">
        <temp @emit-method='fatherClick'></temp>
    </div>
</body>
<script src="../vue.js"></script>

<template id="tmp">
    <div>
        <button v-for="item in categories" @click="sonBtnClick(item)">{{item.name}}</button>      
    </div>
</template>

<script>
    let temp = {
        template: '#tmp',
        data() {
            return {
                categories: [{
                    id: 'aaa',
                    name: '热门推荐'
                }, {
                    id: 'bbb',
                    name: '手机数码'
                }, {
                    id: 'ccc',
                    name: '家居家具'
                }, {
                    id: 'ddd',
                    name: '电脑办公'
                }]
            }
        },
        methods: {
            sonBtnClick(item) {
                //console.log(item)
                this.$emit('emit-method')
            }
        }
    }

    const vm = new Vue({
        el: '#app',
        components: {
            temp
        },
        methods: {
            fatherClick() {
                alert('我是通过子组件 $emit 向外发射触发的方法...')
            }
        }
    })
</script>

</html>